package cn.tedu.erp.service.impl;

import cn.tedu.erp.exception.ServiceException;
import cn.tedu.erp.mapper.UserMapper;
import cn.tedu.erp.pojo.dto.UserLoginDTO;
import cn.tedu.erp.pojo.dto.UserRegDTO;
import cn.tedu.erp.pojo.entity.User;
import cn.tedu.erp.pojo.vo.UserVO;
import cn.tedu.erp.response.JsonResult;
import cn.tedu.erp.response.StatusCode;
import cn.tedu.erp.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserService implements IUserService {

    @Autowired
    UserMapper mapper;


    @Override
    public void reg(UserRegDTO userRegDTO) {
        UserVO userVO=mapper.selectByTNumber(userRegDTO.getTelephoneNumber());
        if (userVO!=null){
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        mapper.insert(user);
    }

    @Override
    public UserVO login(UserLoginDTO userLoginDTO) {
        UserVO userVO = mapper.selectByTNumber(userLoginDTO.getTelephoneNumber());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(! userLoginDTO.getPassword().equals(userVO.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
}
